﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrTKHangLoai : DevComponents.DotNetBar.Office2007Form
    {
        public FrTKHangLoai()
        {
            InitializeComponent();
        }
        private string MaSP1, MaCT1, TenCT1, SoBB1, KhuVuc1, XuLy1, KhuyetTatGo1, ThieuQC1, Bao1, Toupi1, MongDuc1, KoKhoan1, ChaNham1, LapRap1, Uon1, DuongRuot1, TrayXuoc1, VatCanh1, NutMe1, NutGhep1, RangCua1, LoiLom1, LoiDinh1, KhoanSai1, ThieuLoKhoan1, Lem1, Keo1, Gay1, HoaCai1, CaoThap1, Khac1, Ngay1,child=" - ",Loi1,NguoiPT1,SLLoai1,Ngay_addgrid,GhiChu11,GhiChu12,Ngay_Start,Ngay_End,LoiKhac1,Muc1,Cong1,RapVong1,Mat1;
        private double SLLoai_d = 0;
        private void combo_load()
        {
            Data dta = new Data();
            FormProcess Frp = new FormProcess();
            string sql_load = @"select * from dbo.Load_cbSP";
            Frp.cb1(cb_MaSP, sql_load, 1, 0);
            Frp.cb1(cb_MaSPRp, sql_load, 1, 0);
            
            string[] arr = new string[] { "Khuyết Tật Gỗ - 01", "Thiếu Quy Cách - 02", "Bào - 03", "Toupi - 04", "Mộng Đục - 05", "Ko Khoan - 06","Chà Nhám - 07","Lắp Ráp - 08","Uốn - 09","Đường Ruột - 10","Trầy Xước - 11","Vát Cạnh - 12","Nứt Mẻ - 13","Nứt Ghép - 14","Răng Cưa - 15","Lồi Lõm - 16","Lỗi  Đinh - 17","Khoan Sai - 18","Thiếu Lỗ Khoan - 19","Lem - 20","Keo - 21","Gãy - 22","Hoa Cải - 23","Cao Thấp - 24","Khác - 25" };
            for (int i = 0; i < arr.Length; i++)
            {
                cb_Loi.Items.Add(arr[i].ToString());
            }
            string[] arr_XuLy = new string[] { "Bỏ", "Thay","Tận Dụng", "Tái Chế", "Khác" };
            for (int j = 0; j < arr_XuLy.Length; j++)
            {
                cb_XuLy.Items.Add(arr_XuLy[j].ToString());
                cb_XuLyRp.Items.Add(arr_XuLy[j].ToString());
            }
            string[] arr_KV = new string[] { "KV1", "KV2", "KV3", "KV4","KV5" };
            for (int k = 0; k < arr_KV.Length; k++)
            {
                cb_KhuVucRp.Items.Add(arr_KV[k].ToString());
            }
            string[] arr_Khac = new string[] { "Mục", "Cong", "Hở Path", "Canh Cỡ", "Mắt", "Rập Vồng", "Chẻ Rãnh Sai", "Vát Sai", "Thiếu Vát", "Hụt Dài", "Toupi Sai", "Bo Đầu", "Cắt Sai", "Vát Lớn", "Ghép Hở"};
            for (int i = 0; i < arr_Khac.Length; i++)
            {
                cb_LoiKhac.Items.Add(arr_Khac[i].ToString());
            }

        }
        public void Users(string Users)
        {
            lb_user.Text = Users;
        }
        private void FrTKHangLoai_Load(object sender, EventArgs e)
        {
            combo_load();
        }

        private void cb_MaSP_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            Process prs = new Process();
            FormProcess Frp = new FormProcess();
            MaSP1 = prs.substring_right(cb_MaSP.Text, child);
            cb_MaCT.Items.Clear();
            cb_MaCT.Text = "";
            string sql_load = @"select MaChiTiet,TenChiTiet from dbo.ChiTietSP where MaSP='" + MaSP1 + "'";
            Frp.cb1(cb_MaCT, sql_load, 1, 0);
            Frp.cb1(cb_GhiChu1, sql_load, 1, 0);
        }

        private void txt_KTGo_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_KTGo);
        }

        private void txt_ThieuQC_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_ThieuQC);
        }

        private void txt_Bao_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_Bao);
        }

        private void txt_Toupi_TextChanged(object sender, EventArgs e)
        {
           
            checktext_number(txt_Toupi);
        }

        private void txt_MongDuc_TextChanged(object sender, EventArgs e)
        {
           
            checktext_number(txt_MongDuc);
        }

        private void txt_Khoan_TextChanged(object sender, EventArgs e)
        {
           
            checktext_number(txt_KoKhoan);
        }

        private void txt_ChaNham_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_ChaNham);
        }

        private void txt_LapRap_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_LapRap);
        }

        private void txt_Uon_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_Uon);
        }

        private void txt_DuongRuot_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_DuongRuot);
        }

        private void txt_TrayXuoc_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_TrayXuoc);
        }

        private void txt_VatCanh_TextChanged(object sender, EventArgs e)
        {
            checktext_number(txt_VatCanh);
        }

        private void txt_NutMe_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_NutMe);
        }

        private void txt_NutGhep_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_NutGhep);
        }

        private void txt_RangCua_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_RangCua);
        }

        private void txt_LoiLom_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_LoiLom);
        }

        private void txt_LoiDinh_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_LoiDinh);
        }

        private void txt_KhoanSai_TextChanged(object sender, EventArgs e)
        {
           
            checktext_number(txt_KhoanSai);
        }

        private void txt_ThieuLoKhoan_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_ThieuLoKhoan);
        }

        private void txt_Lem_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_Lem);
        }

        private void txt_Keo_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_Keo);
        }

        private void txt_Gay_TextChanged(object sender, EventArgs e)
        {
           
            checktext_number(txt_Gay);
        }

        private void txt_HoaCai_TextChanged(object sender, EventArgs e)
        {
           
            checktext_number(txt_HoaCai);
        }

        private void txt_CaoThap_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_CaoThap);
        }

        private void txt_Khac_TextChanged(object sender, EventArgs e)
        {
           
            checktext_number(txt_Khac);
        }

        private void txt_SoLoi_TextChanged(object sender, EventArgs e)
        {
            
            checktext_number(txt_SoLoi);
        }

        private void txt_SLLoai_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SLLoai);
        }
        private void txt_Muc_TextChanged(object sender, EventArgs e)
        {
            checktext_number(txt_Muc);
        }

        private void txt_Cong_TextChanged(object sender, EventArgs e)
        {
            checktext_number(txt_Cong);
        }

        private void txt_RapVong_TextChanged(object sender, EventArgs e)
        {
            checktext_number(txt_RapVong);
        }

        private void txt_Mat_TextChanged(object sender, EventArgs e)
        {
            checktext_number(txt_Mat);
        }
        private void insert_textbox()
        {
            if (cb_Loi.Text != "")
            {
                Process prs = new Process();
                Loi1 = prs.substring_right(cb_Loi.Text, child);
                if (Loi1 != "")
                {
                    if (Loi1 == "01")
                        txt_KTGo.Text = txt_SoLoi.Text;
                    if (Loi1 == "02")
                        txt_ThieuQC.Text = txt_SoLoi.Text;
                    if (Loi1 == "03")
                        txt_Bao.Text = txt_SoLoi.Text;
                    if(Loi1=="04")
                        txt_Toupi.Text = txt_SoLoi.Text;
                    if(Loi1=="05")
                        txt_MongDuc.Text = txt_SoLoi.Text;
                    if (Loi1 == "06")
                        txt_KoKhoan.Text = txt_SoLoi.Text;
                    if(Loi1=="07")
                        txt_ChaNham.Text = txt_SoLoi.Text;
                    if(Loi1=="08")
                        txt_LapRap.Text = txt_SoLoi.Text;
                    if (Loi1 == "09")
                        txt_Uon.Text = txt_SoLoi.Text;
                    if (Loi1 == "10")
                        txt_DuongRuot.Text = txt_SoLoi.Text;
                    if (Loi1 == "11")
                        txt_TrayXuoc.Text = txt_SoLoi.Text;
                    if (Loi1 == "12")
                        txt_VatCanh.Text = txt_SoLoi.Text;
                    if (Loi1 == "13")
                        txt_NutMe.Text = txt_SoLoi.Text;
                    if (Loi1 == "14")
                        txt_NutGhep.Text = txt_SoLoi.Text;
                    if (Loi1 == "15")
                        txt_RangCua.Text = txt_SoLoi.Text;
                    if (Loi1 == "16")
                        txt_LoiLom.Text = txt_SoLoi.Text;
                    if (Loi1 == "17")
                        txt_LoiDinh.Text = txt_SoLoi.Text;
                    if (Loi1 == "18")
                        txt_KhoanSai.Text = txt_SoLoi.Text;
                    if (Loi1 == "19")
                        txt_ThieuLoKhoan.Text = txt_SoLoi.Text;
                    if (Loi1 == "20")
                        txt_Lem.Text = txt_SoLoi.Text;
                    if (Loi1 == "21")
                        txt_Keo.Text = txt_SoLoi.Text;
                    if (Loi1 == "22")
                        txt_Gay.Text = txt_SoLoi.Text;
                    if (Loi1 == "23")
                        txt_HoaCai.Text = txt_SoLoi.Text;
                    if (Loi1 == "24")
                        txt_CaoThap.Text = txt_SoLoi.Text;
                    if (Loi1 == "25")
                        txt_Khac.Text = txt_SoLoi.Text;
                    cb_Loi.Focus();
                }
                else
                    MessageBox.Show("Nhập Sai Tên Lỗi");
            }
        }

        private void txt_SoLoi_Leave(object sender, EventArgs e)
        {
            insert_textbox();
        }
        private void add_grid()
        {
            try
            {
                if (cb_MaSP.Text == "" || cb_MaCT.Text == "" || txt_KhuVuc.Text == "" || txt_SLLoai.Text == "" || txt_Ngay.Text == "")
                    MessageBox.Show("Nhập Thiếu Thông Tin");
                else
                {
                    Process prs = new Process();
                    MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                    MaCT1 = prs.substring_right(cb_MaCT.Text, child);
                    TenCT1 = prs.substring_left(cb_MaCT.Text, child);
                    LoiKhac1 = cb_LoiKhac.Text;
                    /*if (MaCT1 == "" || MaSP1 == "" || (txt_KhuVuc.Text != "KV1" && txt_KhuVuc.Text != "KV2" && txt_KhuVuc.Text != "KV3" && txt_KhuVuc.Text != "KV4" && txt_KhuVuc.Text != "KV5"&&txt_KhuVuc.Text!="Kaizen_KV3"&&txt_KhuVuc.Text="Kaizen_KV4))
                        MessageBox.Show("Nhập sai thông tin");*/
            
                        KhuVuc1 = txt_KhuVuc.Text.Trim();
                        SoBB1 = txt_SoBB.Text.Trim();
                        XuLy1 = cb_XuLy.Text.Trim();
                        NguoiPT1 = txt_NguoiPT.Text.Trim();
                        string[] temp = txt_Ngay.Text.Split('/');
                        Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                        SLLoai1 = txt_SLLoai.Text;
                        Ngay_addgrid = txt_Ngay.Text;
                        
                        if (txt_KTGo.Text == "")
                            KhuyetTatGo1 = "0";
                        else
                            KhuyetTatGo1 = txt_KTGo.Text;
                        if (txt_ThieuQC.Text == "")
                            ThieuQC1 = "0";
                        else
                            ThieuQC1 = txt_ThieuQC.Text;
                        if (txt_Bao.Text == "")
                            Bao1 = "0";
                        else
                            Bao1 = txt_Bao.Text;
                        if (txt_Toupi.Text == "")
                            Toupi1 = "0";
                        else
                            Toupi1 = txt_Toupi.Text;
                        if (txt_MongDuc.Text == "")
                            MongDuc1 = "0";
                        else
                            MongDuc1 = txt_MongDuc.Text;
                        if (txt_KoKhoan.Text == "")
                            KoKhoan1 = "0";
                        else
                            KoKhoan1 = txt_KoKhoan.Text;
                        if (txt_ChaNham.Text == "")
                            ChaNham1 = "0";
                        else
                            ChaNham1 = txt_ChaNham.Text;
                        if (txt_LapRap.Text == "")
                            LapRap1 = "0";
                        else
                            LapRap1 = txt_LapRap.Text;
                        if (txt_Uon.Text == "")
                            Uon1 = "0";
                        else
                            Uon1 = txt_Uon.Text;
                        if (txt_DuongRuot.Text == "")
                            DuongRuot1 = "0";
                        else
                            DuongRuot1 = txt_DuongRuot.Text;
                        if (txt_TrayXuoc.Text == "")
                            TrayXuoc1 = "0";
                        else
                            TrayXuoc1 = txt_TrayXuoc.Text;
                        if (txt_VatCanh.Text == "")
                            VatCanh1 = "0";
                        else
                            VatCanh1 = txt_VatCanh.Text;
                        if (txt_NutMe.Text == "")
                            NutMe1 = "0";
                        else
                            NutMe1 = txt_NutMe.Text;
                        if (txt_NutGhep.Text == "")
                            NutGhep1 = "0";
                        else
                            NutGhep1 = txt_NutGhep.Text;
                        if (txt_RangCua.Text == "")
                            RangCua1 = "0";
                        else
                            RangCua1 = txt_RangCua.Text;
                        if (txt_LoiLom.Text == "")
                            LoiLom1 = "0";
                        else
                            LoiLom1 = txt_LoiLom.Text;
                        if (txt_LoiDinh.Text == "")
                            LoiDinh1 = "0";
                        else
                            LoiDinh1 = txt_LoiDinh.Text;
                        if (txt_KhoanSai.Text == "")
                            KhoanSai1 = "0";
                        else
                            KhoanSai1 = txt_KhoanSai.Text;
                        if (txt_ThieuLoKhoan.Text == "")
                            ThieuLoKhoan1 = "0";
                        else
                            ThieuLoKhoan1 = txt_ThieuLoKhoan.Text;
                        if (txt_Lem.Text == "")
                            Lem1 = "0";
                        else
                            Lem1 = txt_Lem.Text;
                        if (txt_Keo.Text == "")
                            Keo1 = "0";
                        else
                            Keo1 = txt_Keo.Text;
                        if (txt_Gay.Text == "")
                            Gay1 = "0";
                        else
                            Gay1 = txt_Gay.Text;
                        if (txt_HoaCai.Text == "")
                            HoaCai1 = "0";
                        else
                            HoaCai1 = txt_HoaCai.Text;
                        if (txt_CaoThap.Text == "")
                            CaoThap1 = "0";
                        else
                            CaoThap1 = txt_CaoThap.Text;
                        if (txt_Muc.Text == "")
                            Muc1 = "0";
                        else
                            Muc1 = txt_Muc.Text;
                        if (txt_Cong.Text == "")
                            Cong1 = "0";
                        else
                            Cong1 = txt_Cong.Text;
                        if (txt_RapVong.Text == "")
                            RapVong1 = "0";
                        else
                            RapVong1 = txt_RapVong.Text;
                        if (txt_Mat.Text == "")
                            Mat1 = "0";
                        else
                            Mat1 = txt_Mat.Text;
                        if (txt_Khac.Text == "")
                            Khac1 = "0";
                        else
                            Khac1 = txt_Khac.Text;
                        if (cb_GhiChu1.Text != "")
                            GhiChu11 = prs.substring_left(cb_GhiChu1.Text, child);
                        else
                            GhiChu11 = "";
                        if (cb_GhiChu2.Text != "")
                            GhiChu12 = prs.substring_left(cb_GhiChu2.Text, child);
                        else
                            GhiChu12 = "";
                        if (rd_ChiTiet.Checked == true)
                        {
                            dataGridView2.Rows.Add(MaCT1,TenCT1, KhuVuc1, NguoiPT1, SoBB1, SLLoai1, KhuyetTatGo1, ThieuQC1, Bao1, Toupi1, MongDuc1, KoKhoan1, ChaNham1, LapRap1, Uon1, DuongRuot1, TrayXuoc1, VatCanh1, NutMe1, NutGhep1, RangCua1, LoiLom1, LoiDinh1, KhoanSai1, ThieuLoKhoan1, Lem1, Keo1, Gay1, HoaCai1, CaoThap1,Muc1,Cong1,RapVong1,Mat1, Khac1,LoiKhac1, XuLy1, Ngay_addgrid, GhiChu11, GhiChu12, Ngay1);
                            prs.reset_txt(groupPanel2);
                            txt_SLLoai.Text = "";
                            cb_MaSP.Focus();
                        }
                        
                        if (rd_Ve.Checked == true)
                        {
                            string select = @"select MaChiTiet,TenChiTiet,SoLuongCT from dbo.ChiTietSP where dbo.ChiTietSP.Ve='"+MaCT1+"'";
                            if (cb_XuLy.Text == "Bỏ")
                            {
                                DataSet ds = prs.dts(select);
                                if (ds.Tables[0].Rows.Count < 1)
                                {
                                    MessageBox.Show("Vế Không Đúng");
                                }
                                else
                                {
                                    if (GhiChu11 == "" && GhiChu12 == "")
                                        MessageBox.Show("Nhập thông tin ở phần ghi chú cho Vế bị bỏ");
                                    else
                                    {
                                        int SLCT_Loai;
                                        int SLLoai_Ve = int.Parse(SLLoai1);
                                        int SoLuongCT_int = 0;
                                        dataGridView2.Rows.Add(MaCT1,TenCT1, KhuVuc1, NguoiPT1, SoBB1, SLLoai1, KhuyetTatGo1, ThieuQC1, Bao1, Toupi1, MongDuc1, KoKhoan1, ChaNham1, LapRap1, Uon1, DuongRuot1, TrayXuoc1, VatCanh1, NutMe1, NutGhep1, RangCua1, LoiLom1, LoiDinh1, KhoanSai1, ThieuLoKhoan1, Lem1, Keo1, Gay1, HoaCai1, CaoThap1,Muc1,Cong1,RapVong1,Mat1, Khac1,LoiKhac1, XuLy1, Ngay_addgrid, GhiChu11, GhiChu12, Ngay1);
                                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                        {
                                            MaCT1 = ds.Tables[0].Rows[i]["MaChiTiet"].ToString();
                                            SoLuongCT_int = int.Parse(ds.Tables[0].Rows[i]["SoLuongCT"].ToString());
                                            TenCT1 = ds.Tables[0].Rows[i]["TenChiTiet"].ToString();
                                            SLCT_Loai = SoLuongCT_int * SLLoai_Ve;
                                            dataGridView2.Rows.Add(MaCT1, TenCT1, KhuVuc1, NguoiPT1, SoBB1, SLCT_Loai, "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", LoiKhac1, XuLy1, Ngay_addgrid, "", "", Ngay1);
                                            prs.reset_txt(groupPanel2);
                                            txt_SLLoai.Text = "";
                                            cb_MaSP.Focus();
                                        }
                                    }
                                }

                            }
                            else
                            {
                                MessageBox.Show("Option Quy Đổi chỉ sử dụng cho Vế bị bỏ");
                            }
                        }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
        private void bt_insert_Click(object sender, EventArgs e)
        {
            add_grid();
        }
        private void save()
        {
            int i;
                Process prs = new Process();
                SqlConnection Conn = prs.Connect();
                SqlCommand cmd = new SqlCommand();
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                string insert = null;
                if (dataGridView2.Rows.Count > 1)
                {
                    for (i = dataGridView2.Rows.Count - 2; i >= 0; i--)
                    {
                        MaCT1 = dataGridView2.Rows[i].Cells["MaCT"].Value.ToString();
                        KhuVuc1 = dataGridView2.Rows[i].Cells["KhuVuc"].Value.ToString();
                        SoBB1 = dataGridView2.Rows[i].Cells["SoBB"].Value.ToString();
                        SLLoai1 = dataGridView2.Rows[i].Cells["SLLoai"].Value.ToString();
                        SLLoai_d = double.Parse(SLLoai1);
                        XuLy1 = dataGridView2.Rows[i].Cells["BienPhap"].Value.ToString();
                        NguoiPT1 = dataGridView2.Rows[i].Cells["NguoiPT"].Value.ToString();
                        Ngay1 = dataGridView2.Rows[i].Cells["Ngayinsert"].Value.ToString();
                        KhuyetTatGo1 = dataGridView2.Rows[i].Cells["KhuyetTatGo"].Value.ToString();
                        ThieuQC1 = dataGridView2.Rows[i].Cells["ThieuQuyCach"].Value.ToString();
                        Bao1 = dataGridView2.Rows[i].Cells["Bao"].Value.ToString();
                        MongDuc1 = dataGridView2.Rows[i].Cells["MongDuc"].Value.ToString();
                        Toupi1 = dataGridView2.Rows[i].Cells["Toupi"].Value.ToString();
                        KoKhoan1 = dataGridView2.Rows[i].Cells["KhongLoKhoan"].Value.ToString();
                        ChaNham1 = dataGridView2.Rows[i].Cells["ChaNham"].Value.ToString();
                        LapRap1 = dataGridView2.Rows[i].Cells["LapRap"].Value.ToString();
                        Uon1 = dataGridView2.Rows[i].Cells["Uon"].Value.ToString();
                        DuongRuot1 = dataGridView2.Rows[i].Cells["DuongRuot"].Value.ToString();
                        TrayXuoc1 = dataGridView2.Rows[i].Cells["TrayXuoc"].Value.ToString();
                        VatCanh1 = dataGridView2.Rows[i].Cells["VatCanh"].Value.ToString();
                        NutMe1 = dataGridView2.Rows[i].Cells["NutMe"].Value.ToString();
                        NutGhep1 = dataGridView2.Rows[i].Cells["NutGhep"].Value.ToString();
                        RangCua1 = dataGridView2.Rows[i].Cells["RangCua"].Value.ToString();
                        LoiLom1 = dataGridView2.Rows[i].Cells["LoiLom"].Value.ToString();
                        LoiDinh1 = dataGridView2.Rows[i].Cells["LoiDinh"].Value.ToString();
                        KhoanSai1 = dataGridView2.Rows[i].Cells["KhoanSai"].Value.ToString();
                        ThieuLoKhoan1 = dataGridView2.Rows[i].Cells["ThieuLoKhoan"].Value.ToString();
                        Lem1 = dataGridView2.Rows[i].Cells["Lem"].Value.ToString();
                        Keo1 = dataGridView2.Rows[i].Cells["Keo"].Value.ToString();
                        Gay1 = dataGridView2.Rows[i].Cells["Gay"].Value.ToString();
                        HoaCai1 = dataGridView2.Rows[i].Cells["HoaCai"].Value.ToString();
                        CaoThap1 = dataGridView2.Rows[i].Cells["CaoThap"].Value.ToString();
                        Muc1 = dataGridView2.Rows[i].Cells["Muc"].Value.ToString();
                        Cong1 = dataGridView2.Rows[i].Cells["Cong"].Value.ToString();
                        RapVong1 = dataGridView2.Rows[i].Cells["RapVong"].Value.ToString();
                        Mat1 = dataGridView2.Rows[i].Cells["Mat"].Value.ToString();
                        Khac1 = dataGridView2.Rows[i].Cells["Khac"].Value.ToString();
                        LoiKhac1 = dataGridView2.Rows[i].Cells["LoiKhac"].Value.ToString();
                        GhiChu11 = dataGridView2.Rows[i].Cells["GhiChu1"].Value.ToString();
                        GhiChu12 = dataGridView2.Rows[i].Cells["GhiChu2"].Value.ToString();
                        insert = @"INSERT INTO [dbo].[TK_HangLoai]
                    ([MaCT],[KhuVuc],[NguoiPT],[SoBB],[SLLoai],[KhuyetTatGo],[ThieuQuyCach],[Bao],[Toupi],[MongDuc],[KhongLoKhoan]
                    ,[ChaNham],[LapRap],[Uon],[DuongRuot],[TrayXuoc],[VatCanh],[NutMe],[NutGhep],[RangCua],[LoiLom]
                    ,[LoiDinh],[KhoanSai],[ThieuLoKhoan],[Lem],[Keo],[Gay],[HoaCai],[CaoThap],[Muc],[Cong],[RapVong],[Mat],[Khac],[LoiKhac],[BienPhap],[Ngay],[GhiChu1],[GhiChu2]) values(N'";
                        insert += MaCT1 + "',N'" + KhuVuc1 + "',N'" + NguoiPT1 + "','" + SoBB1 + "'," + SLLoai_d + "," + KhuyetTatGo1 + "," + ThieuQC1 + "," + Bao1 + "," + Toupi1 + "," + MongDuc1 + "," + KoKhoan1 + "," + ChaNham1 + "," + LapRap1 + "," + Uon1 + ",";
                        insert += DuongRuot1 + "," + TrayXuoc1 + "," + VatCanh1 + "," + NutMe1 + "," + NutGhep1 + "," + RangCua1 + "," + LoiLom1 + "," + LoiDinh1 + "," + KhoanSai1 + "," + ThieuLoKhoan1 + "," + Lem1 + "," + Keo1 + "," + Gay1 + "," + HoaCai1 + "," + CaoThap1 + "," + Muc1 + "," + Cong1 + "," + RapVong1 + "," + Mat1 + "," + Khac1 + ",N'" + LoiKhac1 + "',N'";
                        insert += XuLy1 + "','" + Ngay1 + "',N'" + GhiChu11 + "',N'" + GhiChu12 + "')";
                        //MessageBox.Show(insert);
                        cmd.CommandText = insert;
                        cmd.ExecuteNonQuery();
                        prs.reset_txt(groupPanel2);
                        txt_SLLoai.Text = "";
                        dataGridView2.Rows.RemoveAt(i);
                        cb_MaSP.Focus();
                    }

                    MessageBox.Show("Dữ liệu được lưu thành công");
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
            }

        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            save();
        }
        private void search()
        {
            try
            {
                string search = null;
                Process prs = new Process();
                prs.reset_txt(groupPanel2);
                if (txt_SoBB.Text != "")
                {
                    search = @"SELECT     TenSP, TenChiTiet, MaCT, KhuVuc, NguoiPT, SoBB, SLLoai, KhuyetTatGo, ThieuQuyCach, Bao, Toupi, MongDuc, KhongLoKhoan, ChaNham, LapRap, 
                      Uon, DuongRuot, TrayXuoc, VatCanh, NutMe, NutGhep, RangCua, LoiLom, LoiDinh, KhoanSai, ThieuLoKhoan, Lem, Keo, Gay, HoaCai, CaoThap,Muc,Cong,RapVong,Mat, Khac,LoiKhac,
                      BienPhap, CONVERT(char(10), Ngay, 103) AS Ngay, Serial, SoLuongCT, ChieuDaySC, ChieuRongSC, ChieuDaiSC,GhiChu1,GhiChu2
                        FROM         dbo.TK_HangLoai_CT where SoBB='" + txt_SoBB.Text + "'";

                }
                else
                {
                    if (txt_Ngay.Text != "" && cb_MaCT.Text != "" & txt_KhuVuc.Text != "")
                    {
                        MaCT1 = prs.substring_right(cb_MaCT.Text, child);
                        if (MaCT1 == "")
                            MessageBox.Show("Nhập sai thông tin tìm kiếm");
                        else
                        {
                            string[] temp = txt_Ngay.Text.Split('/');
                            Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                            search = @"select TenSP, TenChiTiet, MaCT, KhuVuc, NguoiPT, SoBB, SLLoai, KhuyetTatGo, ThieuQuyCach, Bao, Toupi, MongDuc, KhongLoKhoan, ChaNham, LapRap, 
                      Uon, DuongRuot, TrayXuoc, VatCanh, NutMe, NutGhep, RangCua, LoiLom, LoiDinh, KhoanSai, ThieuLoKhoan, Lem, Keo, Gay, HoaCai, CaoThap,Muc,Cong,RapVong,Mat, Khac,LoiKhac,
                      BienPhap, CONVERT(char(10), Ngay, 103) AS Ngay, Serial, SoLuongCT, ChieuDaySC, ChieuRongSC, ChieuDaiSC,GhiChu1,GhiChu2
                        FROM         dbo.TK_HangLoai_CT where Ngay='" + Ngay1 + "' and MaCT='" + MaCT1 + "' and KhuVuc='" + txt_KhuVuc.Text + "'";
                        }
                    }
                    else
                        MessageBox.Show("Nhập đầy đủ: Ngày, Mã CT,KV để tìm");
                }
                DataSet ds = prs.dts(search);
                dataGridView1.DataSource = ds.Tables[0];
                if (dataGridView1.Rows.Count > 1)
                {
                    int i = dataGridView1.Rows.Count - 2;
                    //MessageBox.Show(i.ToString());
                    Display(i);
                }

                else
                {
                    lb_serial.Text = "0";
                    MessageBox.Show("Không có dữ liệu");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_search_Click(object sender, EventArgs e)
        {
            search();
        }
        private void Display(int i)
        {
            try
            {
                lb_serial.Text = dataGridView1.Rows[i].Cells["Serial"].Value.ToString();
                cb_MaSP.Text = dataGridView1.Rows[i].Cells["TenSP"].Value.ToString();
                cb_MaCT.Text = dataGridView1.Rows[i].Cells["TenChiTiet"].Value.ToString() + " - " + dataGridView1.Rows[i].Cells["MaCT"].Value.ToString();
                txt_KhuVuc.Text = dataGridView1.Rows[i].Cells["KhuVuc"].Value.ToString();
                txt_SoBB.Text = dataGridView1.Rows[i].Cells["SoBB"].Value.ToString();
                txt_SLLoai.Text = dataGridView1.Rows[i].Cells["SLLoai"].Value.ToString();
                cb_XuLy.Text = dataGridView1.Rows[i].Cells["BienPhap"].Value.ToString();
                txt_NguoiPT.Text = dataGridView1.Rows[i].Cells["NguoiPT"].Value.ToString();
                txt_Ngay.Text = dataGridView1.Rows[i].Cells["Ngay"].Value.ToString();
                txt_KTGo.Text = dataGridView1.Rows[i].Cells["KhuyetTatGo"].Value.ToString();
                txt_ThieuQC.Text = dataGridView1.Rows[i].Cells["ThieuQuyCach"].Value.ToString();
                txt_Bao.Text = dataGridView1.Rows[i].Cells["Bao"].Value.ToString();
                txt_MongDuc.Text = dataGridView1.Rows[i].Cells["MongDuc"].Value.ToString();
                txt_Toupi.Text = dataGridView1.Rows[i].Cells["Toupi"].Value.ToString();
                txt_KoKhoan.Text = dataGridView1.Rows[i].Cells["KhongLoKhoan"].Value.ToString();
                txt_ChaNham.Text = dataGridView1.Rows[i].Cells["ChaNham"].Value.ToString();
                txt_LapRap.Text = dataGridView1.Rows[i].Cells["LapRap"].Value.ToString();
                txt_Uon.Text = dataGridView1.Rows[i].Cells["Uon"].Value.ToString();
                txt_DuongRuot.Text = dataGridView1.Rows[i].Cells["DuongRuot"].Value.ToString();
                txt_TrayXuoc.Text = dataGridView1.Rows[i].Cells["TrayXuoc"].Value.ToString();
                txt_VatCanh.Text = dataGridView1.Rows[i].Cells["VatCanh"].Value.ToString();
                txt_NutMe.Text = dataGridView1.Rows[i].Cells["NutMe"].Value.ToString();
                txt_NutGhep.Text = dataGridView1.Rows[i].Cells["NutGhep"].Value.ToString();
                txt_RangCua.Text = dataGridView1.Rows[i].Cells["RangCua"].Value.ToString();
                txt_LoiLom.Text = dataGridView1.Rows[i].Cells["LoiLom"].Value.ToString();
                txt_LoiDinh.Text = dataGridView1.Rows[i].Cells["LoiDinh"].Value.ToString();
                txt_KhoanSai.Text = dataGridView1.Rows[i].Cells["KhoanSai"].Value.ToString();
                txt_ThieuLoKhoan.Text = dataGridView1.Rows[i].Cells["ThieuLoKhoan"].Value.ToString();
                txt_Lem.Text = dataGridView1.Rows[i].Cells["Lem"].Value.ToString();
                txt_Keo.Text = dataGridView1.Rows[i].Cells["Keo"].Value.ToString();
                txt_Gay.Text = dataGridView1.Rows[i].Cells["Gay"].Value.ToString();
                txt_HoaCai.Text = dataGridView1.Rows[i].Cells["HoaCai"].Value.ToString();
                txt_CaoThap.Text = dataGridView1.Rows[i].Cells["CaoThap"].Value.ToString();
                txt_Muc.Text = dataGridView1.Rows[i].Cells["Muc"].Value.ToString();
                txt_Cong.Text=dataGridView1.Rows[i].Cells["Cong"].Value.ToString();
                txt_RapVong.Text = dataGridView1.Rows[i].Cells["RapVong"].Value.ToString();
                txt_Mat.Text = dataGridView1.Rows[i].Cells["Mat"].Value.ToString();
                txt_Khac.Text = dataGridView1.Rows[i].Cells["Khac"].Value.ToString();
                cb_GhiChu1.Text = dataGridView1.Rows[i].Cells["GhiChu1"].Value.ToString();
                cb_GhiChu2.Text = dataGridView1.Rows[i].Cells["GhiChu2"].Value.ToString();
                cb_LoiKhac.Text = dataGridView1.Rows[i].Cells["LoiKhac"].Value.ToString();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void bt_prev_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_next_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i < dataGridView1.Rows.Count - 2)
                    {
                        i++;
                        Display(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void edit()
        {
             DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa Thông Tin có số serial " + lb_serial.Text, "Thông Báo", MessageBoxButtons.OKCancel);
            if (d == DialogResult.OK)
            {
                try
                {
                    Process prs = new Process();
                    MaCT1 = prs.substring_right(cb_MaCT.Text, child);
                    if (MaCT1 == "" || txt_KhuVuc.Text == "" || txt_SLLoai.Text == "" || txt_Ngay.Text == "")
                        MessageBox.Show("Thông tin nhập thiếu hoặc sai");
                    else
                    {
                        SLLoai_d = double.Parse(txt_SLLoai.Text);
                        if (txt_KTGo.Text == "")
                            KhuyetTatGo1 = "0";
                        else
                            KhuyetTatGo1 = txt_KTGo.Text;
                        if (txt_ThieuQC.Text == "")
                            ThieuQC1 = "0";
                        else
                            ThieuQC1 = txt_ThieuQC.Text;
                        if (txt_Bao.Text == "")
                            Bao1 = "0";
                        else
                            Bao1 = txt_Bao.Text;
                        if (txt_Toupi.Text == "")
                            Toupi1 = "0";
                        else
                            Toupi1 = txt_Toupi.Text;
                        if (txt_MongDuc.Text == "")
                            MongDuc1 = "0";
                        else
                            MongDuc1 = txt_MongDuc.Text;
                        if (txt_KoKhoan.Text == "")
                            KoKhoan1 = "0";
                        else
                            KoKhoan1 = txt_KoKhoan.Text;
                        if (txt_ChaNham.Text == "")
                            ChaNham1 = "0";
                        else
                            ChaNham1 = txt_ChaNham.Text;
                        if (txt_LapRap.Text == "")
                            LapRap1 = "0";
                        else
                            LapRap1 = txt_LapRap.Text;
                        if (txt_Uon.Text == "")
                            Uon1 = "0";
                        else
                            Uon1 = txt_Uon.Text;
                        if (txt_DuongRuot.Text == "")
                            DuongRuot1 = "0";
                        else
                            DuongRuot1 = txt_DuongRuot.Text;
                        if (txt_TrayXuoc.Text == "")
                            TrayXuoc1 = "0";
                        else
                            TrayXuoc1 = txt_TrayXuoc.Text;
                        if (txt_VatCanh.Text == "")
                            VatCanh1 = "0";
                        else
                            VatCanh1 = txt_VatCanh.Text;
                        if (txt_NutMe.Text == "")
                            NutMe1 = "0";
                        else
                            NutMe1 = txt_NutMe.Text;
                        if (txt_NutGhep.Text == "")
                            NutGhep1 = "0";
                        else
                            NutGhep1 = txt_NutGhep.Text;
                        if (txt_RangCua.Text == "")
                            RangCua1 = "0";
                        else
                            RangCua1 = txt_RangCua.Text;
                        if (txt_LoiLom.Text == "")
                            LoiLom1 = "0";
                        else
                            LoiLom1 = txt_LoiLom.Text;
                        if (txt_LoiDinh.Text == "")
                            LoiDinh1 = "0";
                        else
                            LoiDinh1 = txt_LoiDinh.Text;
                        if (txt_KhoanSai.Text == "")
                            KhoanSai1 = "0";
                        else
                            KhoanSai1 = txt_KhoanSai.Text;
                        if (txt_ThieuLoKhoan.Text == "")
                            ThieuLoKhoan1 = "0";
                        else
                            ThieuLoKhoan1 = txt_ThieuLoKhoan.Text;
                        if (txt_Lem.Text == "")
                            Lem1 = "0";
                        else
                            Lem1 = txt_Lem.Text;
                        if (txt_Keo.Text == "")
                            Keo1 = "0";
                        else
                            Keo1 = txt_Keo.Text;
                        if (txt_Gay.Text == "")
                            Gay1 = "0";
                        else
                            Gay1 = txt_Gay.Text;
                        if (txt_HoaCai.Text == "")
                            HoaCai1 = "0";
                        else
                            HoaCai1 = txt_HoaCai.Text;
                        if (txt_CaoThap.Text == "")
                            CaoThap1 = "0";
                        else
                            CaoThap1 = txt_CaoThap.Text;
                        if (txt_Muc.Text == "")
                            Muc1 = "0";
                        else
                            Muc1 = txt_Muc.Text;
                        if (txt_Cong.Text == "")
                            Cong1 = "0";
                        else
                            Cong1 = txt_Cong.Text;
                        if (txt_RapVong.Text == "")
                            RapVong1 = "0";
                        else
                            RapVong1 = txt_RapVong.Text;
                        if (txt_Mat.Text == "")
                            Mat1 = "0";
                        else
                            Mat1 = txt_Mat.Text;
                        if (txt_Khac.Text == "")
                            Khac1 = "0";
                        else
                            Khac1 = txt_Khac.Text;
                        string[] temp = txt_Ngay.Text.Split('/');
                        Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                        string edit = @"UPDATE [dbo].[TK_HangLoai]
                                       SET [MaCT] = '" + MaCT1 + "',[KhuVuc] = '" + txt_KhuVuc.Text + "',[NguoiPT] = '" + txt_NguoiPT.Text + "',[SoBB] = '" + txt_SoBB.Text + "',[SLLoai] = " + SLLoai_d + ",[KhuyetTatGo] = " + KhuyetTatGo1 + ",[ThieuQuyCach] = " + ThieuQC1 + ",[Bao] = " + Bao1 + ",[Toupi] = " + Toupi1 + ",[MongDuc] = " + MongDuc1 + ",[KhongLoKhoan] = " + KoKhoan1;
                        edit += ",[ChaNham] = " + ChaNham1 + ",[LapRap] = " + LapRap1 + ",[Uon] = " + Uon1 + ",[DuongRuot] = " + DuongRuot1 + ",[TrayXuoc] = " + TrayXuoc1 + ",[VatCanh] = " + VatCanh1 + ",[NutMe] = " + NutMe1 + ",[NutGhep] =" + NutGhep1;
                        edit += ",[RangCua] = " + RangCua1 + ",[LoiLom] =" + LoiLom1 + ",[LoiDinh] = " + LoiDinh1 + ",[KhoanSai] = " + KhoanSai1 + ",[ThieuLoKhoan] = " + ThieuLoKhoan1 + ",[Lem] =" + Lem1;
                        edit += ",[Keo] =" + Keo1 + ",[Gay] = " + Gay1 + ",[HoaCai] =" + HoaCai1 + ",[CaoThap] = " + CaoThap1 +",[Muc]="+Muc1+",[Cong]="+Cong1+",[RapVong]="+RapVong1+",[Mat]="+Mat1+",[Khac] =" + Khac1 + ",[LoiKhac]=N'"+cb_LoiKhac.Text+"',[BienPhap] =N'" + cb_XuLy.Text + "',[Ngay] = '" + Ngay1 + "'";
                        edit += " where Serial=" + lb_serial.Text;
                        //MessageBox.Show(edit);
                        SqlConnection Conn = prs.Connect();
                        SqlCommand cmd = new SqlCommand();
                        Conn.Open();
                        cmd.Connection = Conn;
                        cmd.CommandText = edit;
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Chỉnh sửa thành công");
                        prs.reset_txt(groupPanel2);
                        Conn.Close();
                        cmd.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } 
        }

        private void bt_edit_Click(object sender, EventArgs e)
        {
            edit();
        }

        private void FrTKHangLoai_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F11)
                save();
            if (ex.KeyCode == Keys.PageDown)
                bt_insert_Click(null, null);
        }

        private void cb_GhiChu1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Process prs = new Process();
                FormProcess Frp = new FormProcess();
                MaSP1 = prs.substring_right(cb_MaSP.Text, child);
                string sql_load = @"select MaChiTiet,TenChiTiet from dbo.ChiTietSP where MaSP='" + MaSP1 + "'";
                Frp.cb1(cb_GhiChu2, sql_load, 1, 0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rd_Ve_CheckedChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Option Quy Đổi này sẽ quy đổi từ vế ra các chi tiết tương ứng trong vế");
        }
        private void ExportExcel_Nhap(DataTable dt, string sheetName)
        {
            Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
            ExcelObj.Visible = true;
            Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(@"C:\\Huynh\\TKHangLoai", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlNormalLoad);
            Microsoft.Office.Interop.Excel.Sheets sheet = theWorkbook.Worksheets;
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheet.get_Item(1);
            //worksheet.Activate();
            object[,] arr = new object[dt.Columns.Count, dt.Rows.Count];
            //Chuyển dữ liệu từ DataTable vào mảng đối tượng
            for (int c = 0; c < dt.Columns.Count; c++)
            {
                //DataColumn dr = dt.Columns[r];
                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    DataRow dr = dt.Rows[r];
                    arr[c, r] = dr[c];
                }
            }

            //Thiết lập vùng điền dữ liệu
            int rowStart = 6;
            int columnStart = 2;

            //int rowEnd = rowStart + dt.Rows.Count - 1;
            int rowEnd = rowStart + dt.Columns.Count - 1;

            //int columnEnd = dt.Columns.Count;
            int columnEnd = dt.Rows.Count - 1 + columnStart;

            // Ô bắt đầu điền dữ liệu
            Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[rowStart, columnStart];
            // Ô kết thúc điền dữ liệu
            Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[rowEnd, columnEnd];
            // Lấy về vùng điền dữ liệu
            Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(c1, c2);

            //Điền dữ liệu vào vùng đã thiết lập
            range.Value2 = arr;
            //range.Columns.AutoFit();
            range.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
            //theWorkbook=theWorkbook.Open("C:\\Huynh\\test.xlsx");
            //worksheet = ("sheet1");
            //range.Activate();
            object misValue = System.Reflection.Missing.Value;
            theWorkbook.Close(true, misValue, misValue);
            ExcelObj.Quit();
        }
        private void ExportExcel(DataTable dt, string sheetName)
        {

            Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
            ExcelObj.Visible = true;
            Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(@"C:\\Huynh\\HangLoai_SK.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlNormalLoad);
            Microsoft.Office.Interop.Excel.Sheets sheet = theWorkbook.Worksheets;
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheet.get_Item(1);
            //worksheet.Activate();
            object[,] arr = new object[dt.Rows.Count, dt.Columns.Count];
            //Chuyển dữ liệu từ DataTable vào mảng đối tượng
            for (int r = 0; r < dt.Rows.Count; r++)
            {
                DataRow dr = dt.Rows[r];
                for (int c = 0; c < dt.Columns.Count; c++)
                {
                    //DataRow dr = dt.Rows[r];
                    arr[r, c] = dr[c];
                }
            }

            //Thiết lập vùng điền dữ liệu
            int rowStart = 6;
            int columnStart = 1;

            int rowEnd = rowStart + dt.Rows.Count - 1;
            //int rowEnd = rowStart + dt.Columns.Count - 1;

            int columnEnd = columnStart + dt.Columns.Count - 1;
            //int columnEnd = dt.Rows.Count - 1 + columnStart;

            // Ô bắt đầu điền dữ liệu
            Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[rowStart, columnStart];
            // Ô kết thúc điền dữ liệu
            Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[rowEnd, columnEnd];
            // Lấy về vùng điền dữ liệu
            Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(c1, c2);

            //Điền dữ liệu vào vùng đã thiết lập
            range.Value2 = arr;
            //range.Columns.AutoFit();
            range.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
            //theWorkbook=theWorkbook.Open("C:\\test.xlsx");
            //worksheet = ("sheet1");
            //range.Activate();
            object misValue = System.Reflection.Missing.Value;
            theWorkbook.Close(true, misValue, misValue);
            ExcelObj.Quit();

        }
        private void Search_Report()
        {
            Process prs = new Process();
            string select = null;
            DataSet ds = new DataSet();
            try
            {
                if (cb_KhuVucRp.Text != "")
                {
                    if (txt_datestartRp.Text != "" && txt_dateendRp.Text != "")
                    {
                        Ngay_Start = txt_datestartRp.Text.Trim();
                        Ngay_End = txt_dateendRp.Text.Trim();
                        string[] temp = Ngay_Start.Split('/');
                        Ngay_Start = temp[1] + "/" + temp[0] + "/" + temp[2];
                        string[] temp2 = Ngay_End.Split('/');
                        Ngay_End = temp2[1] + "/" + temp2[0] + "/" + temp2[2];
                        if (rd_ThoiGian.Checked == true)
                        {
                            if (cb_XuLyRp.Text == "")
                            {
                                if (cb_MaSPRp.Text == "")
                                {
                                    select = @" select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' order by Ngay";         
                                }
                                else
                                {
                                    MaSP1 = prs.substring_left(cb_MaSPRp.Text, child);
                                    if (MaSP1 == "")
                                        MessageBox.Show("Mã SP Nhập không đúng");
                                    else
                                    {
                                        if (cb_MaCTRp.Text == "")
                                        {
                                            select = @" select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and TenSP=N'" + MaSP1 + "' order by Ngay";
                                        }
                                        if (cb_MaCTRp.Text != "")
                                        {
                                            MaCT1 = prs.substring_right(cb_MaCTRp.Text, child);
                                            if (MaCT1 == "")
                                                MessageBox.Show("Nhập sai thông tin");
                                            else
                                            {
                                                select = @"
                                                            select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and MaCT='"+MaCT1+"' order by Ngay";
                                                
                                            }
                                        }
                                    }
                                }

                            }
                            if (cb_XuLyRp.Text != "")
                            {
                                string XuLyRp = cb_XuLyRp.Text;
                                if (cb_MaSPRp.Text == "")
                                {
                                    select = @"
                                                select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and BienPhap like N'" + XuLyRp + "' order by Ngay";
                                }
                                else
                                {
                                    MaSP1 = prs.substring_left(cb_MaSPRp.Text, child);
                                    if (MaSP1 == "")
                                        MessageBox.Show("Mã SP Nhập không đúng");
                                    else
                                    {
                                        if (cb_MaCTRp.Text == "")
                                        {
                                            select = @"
                                                          select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and BienPhap like N'" + XuLyRp + "' and TenSP=N'"+MaSP1+"' order by Ngay";
                                          
                                        }
                                        if (cb_MaCTRp.Text != "")
                                        {
                                            MaCT1 = prs.substring_right(cb_MaCTRp.Text, child);
                                            if (MaCT1 == "")
                                                MessageBox.Show("Nhập sai thông tin");
                                            else
                                            {
                                                select = @"
                                                           select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and BienPhap='" + XuLyRp + "' and MaCT=N'" + MaCT1 + "' order by Ngay";
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (rd_MucDo.Checked == true)
                        {
                            if (txt_datestartRp.Text != "" && txt_dateendRp.Text != "")
                            {
                                if (cb_XuLyRp.Text == "")
                                {
                                    select = @"SELECT     '' AS Expr2, TenSP, MaCT, TenChiTiet, KhuVuc, SUM(SLLoai) AS SLLoai, BienPhap, SUM(KhuyetTatGo) AS KhuyetTatGo, SUM(ThieuQuyCach) AS ThieuQuyCach, 
                                              SUM(Bao) AS Bao, SUM(Toupi) AS Toupi, SUM(MongDuc) AS MongDuc, SUM(KhongLoKhoan) AS KhongLoKhoan, SUM(ChaNham) 
                                              AS ChaNham, SUM(LapRap) AS LapRap, SUM(Uon) AS Uon, SUM(DuongRuot) AS DuongRuot, SUM(TrayXuoc) AS TrayXuoc, SUM(VatCanh) 
                                              AS VatCanh, SUM(NutMe) AS NutMe, SUM(NutGhep) AS NutGhep, SUM(RangCua) AS RangCua, SUM(LoiLom) AS LoiLom, SUM(LoiDinh) 
                                              AS LoiDinh, SUM(KhoanSai) AS KhoanSai, SUM(ThieuLoKhoan) AS ThieuLoKhoan, SUM(Lem) AS Lem, SUM(Keo) AS Keo, SUM(Gay) AS Gay, 
                                              SUM(HoaCai) AS HoaCai, SUM(CaoThap) AS CaoThap, SUM(Muc) AS Muc, SUM(Cong) AS Cong, SUM(RapVong) AS RapVong, SUM(Mat) 
                                              AS Mat, SUM(Khac) AS Khac, '' AS Expr1, SUM(KhuyetTatGo) / SUM(SLLoai) AS KhuyetTatGo1, SUM(ThieuQuyCach) / SUM(SLLoai) 
                                              AS ThieuQuyCach1, SUM(Bao) / SUM(SLLoai) AS Bao1, SUM(Toupi) / SUM(SLLoai) AS Toupi1, SUM(MongDuc) / SUM(SLLoai) AS MongDuc1, 
                                              SUM(KhongLoKhoan) / SUM(SLLoai) AS KhongLoKhoan1, SUM(ChaNham) / SUM(SLLoai) AS ChaNham1, SUM(LapRap) / SUM(SLLoai) 
                                              AS LapRap1, SUM(Uon) / SUM(SLLoai) AS Uon1, SUM(DuongRuot) / SUM(SLLoai) AS DuongRuot1, SUM(TrayXuoc) / SUM(SLLoai) 
                                              AS TrayXuoc1, SUM(VatCanh) / SUM(SLLoai) AS VatCanh1, SUM(NutMe) / SUM(SLLoai) AS NutMe1, SUM(NutGhep) / SUM(SLLoai) 
                                              AS NutGhep1, SUM(RangCua) / SUM(SLLoai) AS RangCua1, SUM(LoiLom) / SUM(SLLoai) AS LoiLom1, SUM(LoiDinh) / SUM(SLLoai) 
                                              AS LoiDinh1, SUM(KhoanSai) / SUM(SLLoai) AS KhoanSai1, SUM(ThieuLoKhoan) / SUM(SLLoai) AS ThieuLoKhoan1, SUM(Lem) 
                                              / SUM(SLLoai) AS Lem1, SUM(Keo) / SUM(SLLoai) AS Keo1, SUM(Gay) / SUM(SLLoai) AS Gay1, SUM(HoaCai) / SUM(SLLoai) AS HoaCai1, 
                                              SUM(CaoThap) / SUM(SLLoai) AS CaoThap1, SUM(Muc) / SUM(SLLoai) AS Muc1, SUM(Cong) / SUM(SLLoai) AS Cong1, SUM(RapVong) 
                                              / SUM(SLLoai) AS RapVong1, SUM(Mat) / SUM(SLLoai) AS Mat1, SUM(Khac) / SUM(SLLoai) AS Khac1
                                                FROM         dbo.TK_HangLoai_SumLoi
                                                where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' GROUP BY MaCT, KhuVuc, BienPhap,TenSP,TenChiTiet order by SLLoai desc";
                                }
                                if (cb_XuLyRp.Text != "")
                                {
                                    string XuLyRp = cb_XuLyRp.Text;
                                    select = @"SELECT     '' AS Expr2, TenSP, MaCT, TenChiTiet, KhuVuc, SUM(SLLoai) AS SLLoai, BienPhap, SUM(KhuyetTatGo) AS KhuyetTatGo, SUM(ThieuQuyCach) AS ThieuQuyCach, 
                                              SUM(Bao) AS Bao, SUM(Toupi) AS Toupi, SUM(MongDuc) AS MongDuc, SUM(KhongLoKhoan) AS KhongLoKhoan, SUM(ChaNham) 
                                              AS ChaNham, SUM(LapRap) AS LapRap, SUM(Uon) AS Uon, SUM(DuongRuot) AS DuongRuot, SUM(TrayXuoc) AS TrayXuoc, SUM(VatCanh) 
                                              AS VatCanh, SUM(NutMe) AS NutMe, SUM(NutGhep) AS NutGhep, SUM(RangCua) AS RangCua, SUM(LoiLom) AS LoiLom, SUM(LoiDinh) 
                                              AS LoiDinh, SUM(KhoanSai) AS KhoanSai, SUM(ThieuLoKhoan) AS ThieuLoKhoan, SUM(Lem) AS Lem, SUM(Keo) AS Keo, SUM(Gay) AS Gay, 
                                              SUM(HoaCai) AS HoaCai, SUM(CaoThap) AS CaoThap, SUM(Muc) AS Muc, SUM(Cong) AS Cong, SUM(RapVong) AS RapVong, SUM(Mat) 
                                              AS Mat, SUM(Khac) AS Khac, '' AS Expr1, SUM(KhuyetTatGo) / SUM(SLLoai) AS KhuyetTatGo1, SUM(ThieuQuyCach) / SUM(SLLoai) 
                                              AS ThieuQuyCach1, SUM(Bao) / SUM(SLLoai) AS Bao1, SUM(Toupi) / SUM(SLLoai) AS Toupi1, SUM(MongDuc) / SUM(SLLoai) AS MongDuc1, 
                                              SUM(KhongLoKhoan) / SUM(SLLoai) AS KhongLoKhoan1, SUM(ChaNham) / SUM(SLLoai) AS ChaNham1, SUM(LapRap) / SUM(SLLoai) 
                                              AS LapRap1, SUM(Uon) / SUM(SLLoai) AS Uon1, SUM(DuongRuot) / SUM(SLLoai) AS DuongRuot1, SUM(TrayXuoc) / SUM(SLLoai) 
                                              AS TrayXuoc1, SUM(VatCanh) / SUM(SLLoai) AS VatCanh1, SUM(NutMe) / SUM(SLLoai) AS NutMe1, SUM(NutGhep) / SUM(SLLoai) 
                                              AS NutGhep1, SUM(RangCua) / SUM(SLLoai) AS RangCua1, SUM(LoiLom) / SUM(SLLoai) AS LoiLom1, SUM(LoiDinh) / SUM(SLLoai) 
                                              AS LoiDinh1, SUM(KhoanSai) / SUM(SLLoai) AS KhoanSai1, SUM(ThieuLoKhoan) / SUM(SLLoai) AS ThieuLoKhoan1, SUM(Lem) 
                                              / SUM(SLLoai) AS Lem1, SUM(Keo) / SUM(SLLoai) AS Keo1, SUM(Gay) / SUM(SLLoai) AS Gay1, SUM(HoaCai) / SUM(SLLoai) AS HoaCai1, 
                                              SUM(CaoThap) / SUM(SLLoai) AS CaoThap1, SUM(Muc) / SUM(SLLoai) AS Muc1, SUM(Cong) / SUM(SLLoai) AS Cong1, SUM(RapVong) 
                                              / SUM(SLLoai) AS RapVong1, SUM(Mat) / SUM(SLLoai) AS Mat1, SUM(Khac) / SUM(SLLoai) AS Khac1
                                                FROM         dbo.TK_HangLoai_SumLoi
                                                where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and BienPhap like N'"+XuLyRp+"' GROUP BY MaCT, KhuVuc, BienPhap,TenSP,TenChiTiet order by SLLoai desc";
                                }
                            }


                        }
                        if (rd_VeRp.Checked == true)
                        {
                            select = @"select qry1.Ngay,dbo.ChiTietSP.TenSP,qry1.MaCT,dbo.ChiTietSP.TenChiTiet,qry1.KhuVuc, qry1.SLLoai,
                                                    qry1.BienPhap, qry1.KhuyetTatGo,
                                                    qry1.ThieuQuyCach, qry1.Bao, qry1.Toupi, qry1.MongDuc,
                                                    qry1.KhongLoKhoan,qry1.ChaNham, qry1.LapRap, qry1.Uon,
                                                    qry1.DuongRuot, qry1.TrayXuoc, qry1.VatCanh, qry1.NutMe,
                                                    qry1.NutGhep, qry1.RangCua, qry1.LoiLom, qry1.LoiDinh,
                                                    qry1.KhoanSai,qry1.ThieuLoKhoan,qry1.Lem, qry1.Keo, 
                                                    qry1.Gay,qry1.HoaCai,qry1.CaoThap,qry1.Muc,qry1.Cong,qry1.RapVong,qry1.Mat,qry1.Khac,qry1.GhiChu1
                                                from(select Ngay,MaCT,KhuVuc,sum(SLLoai) as SLLoai,BienPhap, SUM(KhuyetTatGo) AS KhuyetTatGo, SUM(ThieuQuyCach) AS ThieuQuyCach, 
                                              SUM(Bao) AS Bao, SUM(Toupi) AS Toupi, SUM(MongDuc) AS MongDuc, SUM(KhongLoKhoan) AS KhongLoKhoan, SUM(ChaNham) 
                                              AS ChaNham, SUM(LapRap) AS LapRap, SUM(Uon) AS Uon, SUM(DuongRuot) AS DuongRuot, SUM(TrayXuoc) AS TrayXuoc, SUM(VatCanh) 
                                              AS VatCanh, SUM(NutMe) AS NutMe, SUM(NutGhep) AS NutGhep, SUM(RangCua) AS RangCua, SUM(LoiLom) AS LoiLom, SUM(LoiDinh) 
                                              AS LoiDinh, SUM(KhoanSai) AS KhoanSai, SUM(ThieuLoKhoan) AS ThieuLoKhoan, SUM(Lem) AS Lem, SUM(Keo) AS Keo, SUM(Gay) AS Gay, 
                                              SUM(HoaCai) AS HoaCai, SUM(CaoThap) AS CaoThap, SUM(Muc) AS Muc, SUM(Cong) AS Cong, SUM(RapVong) AS RapVong, SUM(Mat) 
                                              AS Mat, SUM(Khac) AS Khac,GhiChu1
                                                FROM         dbo.TK_HangLoai where Ngay>='"+Ngay_Start+" 00:00:00' and Ngay<='"+Ngay_End+" 23:59:59' and KhuVuc='"+cb_KhuVucRp.Text+"' GROUP BY MaCT, KhuVuc, BienPhap, Ngay,GhiChu1) as qry1 inner join dbo.ChiTietSP on ChiTietSP.MaChiTiet=qry1.MaCT where dbo.ChiTietSP.Ve like 'V' order by qry1.Ngay";
                           // MessageBox.Show(select);
                        }
                        if (rd_NhapLieu.Checked == true)
                        {

                            select = @"select qry1.Ngay,dbo.ChiTietSP.TenSP,qry1.MaCT, dbo.ChiTietSP.TenChiTiet, qry1.KhuVuc, qry1.SLLoai,
                                                    qry1.BienPhap, qry1.KhuyetTatGo,
                                                    qry1.ThieuQuyCach, qry1.Bao, qry1.Toupi, qry1.MongDuc,
                                                    qry1.KhongLoKhoan,qry1.ChaNham, qry1.LapRap, qry1.Uon,
                                                    qry1.DuongRuot, qry1.TrayXuoc, qry1.VatCanh, qry1.NutMe,
                                                    qry1.NutGhep, qry1.RangCua, qry1.LoiLom, qry1.LoiDinh,
                                                    qry1.KhoanSai,qry1.ThieuLoKhoan,qry1.Lem, qry1.Keo, 
                                                    qry1.Gay,qry1.HoaCai,qry1.CaoThap,qry1.Muc,qry1.Cong,qry1.RapVong,qry1.Mat,qry1.Khac,qry1.GhiChu1
                                                    from (select * from dbo.TK_HangLoai where  Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "') as qry1 inner join dbo.ChiTietSP on ChiTietSP.MaChiTiet=qry1.MaCT order by qry1.Ngay,qry1.MaCT";
                       
                        }
                        if (rd_DoiChieu.Checked == true)
                        {
                            if (cb_KhuVucRp.Text == "KV2")
                            {
                                select = @"select ChiTietSP.TenSP,ChiTietSP.TenChiTiet,CLLXP,SLLoai from
                                        ((select MaChiTiet as MaCT,sum(SLLP)-sum(SLXP) as CLLXP,SLLoai=0
                                            from dbo.KV2_LXP
                                            where NgayXP>='" + Ngay_Start + " 00:00:00 ' and NgayXP<='" + Ngay_End + " 23:59:59' ";
                                select += @"       group by MaChiTiet)
                                            Union all 
                                            (select MaCT,CLLXP=0,sum(SLLoai) as SLLoai from dbo.TK_HangLoai where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='KV2' group by MaCT) ";
                                select += @"                ) as qry1 inner join ChiTietSP on ChiTietSP.MaChiTiet=qry1.MaCT
                                            order by TenSP,TenChiTiet";
                            }
                        }
                        if (rd_SoKhoi.Checked == true)
                        {
                            if (cb_XuLyRp.Text == "")
                            {
                                if (cb_MaSPRp.Text == "")
                                {
                                    select = @"select qry1.Ngay,dbo.ChiTietSP.TenSP, dbo.ChiTietSP.TenChiTiet,ChiTietSP.ChieuDaySC,ChiTietSP.ChieuRongSC,ChiTietSP.ChieuDaiSC, qry1.SLLoai,qry1.SLLoai*ChiTietSP.ChieuDaySC*ChiTietSP.ChieuRongSC*ChiTietSP.ChieuDaiSC/1000000000 as SoKhoi,
                                            qry1.BienPhap, qry1.KhuVuc
                                           
                                            FROM         (select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "') as qry1 INNER JOIN dbo.ChiTietSP ON qry1.MaCT = dbo.ChiTietSP.MaChiTiet order by qry1.Ngay,dbo.ChiTietSP.TenSP";
                                }
                                else
                                {
                                    MaSP1 = prs.substring_right(cb_MaSPRp.Text, child);
                                    if (MaSP1 == "")
                                        MessageBox.Show("Mã SP Nhập không đúng");
                                    else
                                    {
                                        if (cb_MaCTRp.Text == "")
                                        {
                                            select = @"select qry1.Ngay,dbo.ChiTietSP.TenSP, dbo.ChiTietSP.TenChiTiet,ChiTietSP.ChieuDaySC,ChiTietSP.ChieuRongSC,ChiTietSP.ChieuDaiSC, qry1.SLLoai,qry1.SLLoai*ChiTietSP.ChieuDaySC*ChiTietSP.ChieuRongSC*ChiTietSP.ChieuDaiSC/1000000000 as SoKhoi,
                                                        qry1.BienPhap, qry1.KhuVuc
                                                            FROM         (select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "') as qry1 INNER JOIN dbo.ChiTietSP ON qry1.MaCT = dbo.ChiTietSP.MaChiTiet";
                                            select += " where dbo.ChiTietSP.MaSP='" + MaSP1 + "' order by qry1.Ngay,dbo.ChiTietSP.TenSP";
                                        }
                                        if (cb_MaCTRp.Text != "")
                                        {
                                            MaCT1 = prs.substring_right(cb_MaCTRp.Text, child);
                                            if (MaCT1 == "")
                                                MessageBox.Show("Nhập sai thông tin");
                                            else
                                            {
                                                select = @"select qry1.Ngay,dbo.ChiTietSP.TenSP, dbo.ChiTietSP.TenChiTiet,ChiTietSP.ChieuDaySC,ChiTietSP.ChieuRongSC,ChiTietSP.ChieuDaiSC, qry1.SLLoai,qry1.SLLoai*ChiTietSP.ChieuDaySC*ChiTietSP.ChieuRongSC*ChiTietSP.ChieuDaiSC/1000000000 as SoKhoi,
                                                            qry1.BienPhap, qry1.KhuVuc
                                                                            FROM         (select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "') as qry1 INNER JOIN dbo.ChiTietSP ON qry1.MaCT = dbo.ChiTietSP.MaChiTiet";
                                                select += " where qry1.MaCT='" + MaCT1 + "' order by qry1.Ngay,dbo.ChiTietSP.TenSP";
                                            }
                                        }
                                    }
                                }
                            }
                            if (cb_XuLyRp.Text != "")
                            {
                                string XuLyRp = cb_XuLyRp.Text;
                                if (cb_MaSPRp.Text == "")
                                {
                                    select = @"select qry1.Ngay,dbo.ChiTietSP.TenSP, dbo.ChiTietSP.TenChiTiet,ChiTietSP.ChieuDaySC,ChiTietSP.ChieuRongSC,ChiTietSP.ChieuDaiSC, qry1.SLLoai,qry1.SLLoai*ChiTietSP.ChieuDaySC*ChiTietSP.ChieuRongSC*ChiTietSP.ChieuDaiSC/1000000000 as SoKhoi,
                                                            qry1.BienPhap, qry1.KhuVuc
                                            FROM         (select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and BienPhap like N'" + XuLyRp + "' and MaSP=') as qry1 INNER JOIN dbo.ChiTietSP ON qry1.MaCT = dbo.ChiTietSP.MaChiTiet order by qry1.Ngay,dbo.ChiTietSP.TenSP";
                                }
                                else
                                {
                                    MaSP1 = prs.substring_right(cb_MaSPRp.Text, child);
                                    if (MaSP1 == "")
                                        MessageBox.Show("Mã SP Nhập không đúng");
                                    else
                                    {
                                        if (cb_MaCTRp.Text == "")
                                        {
                                            select = @"select qry1.Ngay,dbo.ChiTietSP.TenSP, dbo.ChiTietSP.TenChiTiet,ChiTietSP.ChieuDaySC,ChiTietSP.ChieuRongSC,ChiTietSP.ChieuDaiSC, qry1.SLLoai,qry1.SLLoai*ChiTietSP.ChieuDaySC*ChiTietSP.ChieuRongSC*ChiTietSP.ChieuDaiSC/1000000000 as SoKhoi,
                                                            qry1.BienPhap, qry1.KhuVuc
                                                            FROM         (select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and BienPhap like N'" + XuLyRp + "') as qry1 INNER JOIN dbo.ChiTietSP ON qry1.MaCT = dbo.ChiTietSP.MaChiTiet";
                                            select += " where dbo.ChiTietSP.MaSP='" + MaSP1 + "' order by qry1.Ngay,dbo.ChiTietSP.TenSP";
                                        }
                                        if (cb_MaCTRp.Text != "")
                                        {
                                            MaCT1 = prs.substring_right(cb_MaCTRp.Text, child);
                                            if (MaCT1 == "")
                                                MessageBox.Show("Nhập sai thông tin");
                                            else
                                            {
                                                select = @"select qry1.Ngay,dbo.ChiTietSP.TenSP, dbo.ChiTietSP.TenChiTiet,ChiTietSP.ChieuDaySC,ChiTietSP.ChieuRongSC,ChiTietSP.ChieuDaiSC, qry1.SLLoai,qry1.SLLoai*ChiTietSP.ChieuDaySC*ChiTietSP.ChieuRongSC*ChiTietSP.ChieuDaiSC/1000000000 as SoKhoi,
                                                            qry1.BienPhap, qry1.KhuVuc
                                                                            FROM         (select * from dbo.TK_HangLoai_SumLoi where Ngay>='" + Ngay_Start + " 00:00:00' and Ngay<='" + Ngay_End + " 23:59:59' and KhuVuc='" + cb_KhuVucRp.Text + "' and BienPhap like N'" + XuLyRp + "') as qry1 INNER JOIN dbo.ChiTietSP ON qry1.MaCT = dbo.ChiTietSP.MaChiTiet";
                                                select += " where qry1.MaCT='" + MaCT1 + "' order by qry1.Ngay,dbo.ChiTietSP.TenSP";
                                            }
                                        }
                                    }
                                }
                            }

                        }
                        ds = prs.dts(select);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            
                            if (rd_SoKhoi.Checked == true)
                            {
                                ExportExcel(ds.Tables[0], "FileNhap");
                                ds.Clear();
                            }
                            if (rd_MucDo.Checked == true || rd_NhapLieu.Checked == true || rd_VeRp.Checked == true || rd_ThoiGian.Checked == true)
                            {
                                ExportExcel_Nhap(ds.Tables[0], "FileNhap");
                                ds.Clear();
                            }
                            if (rd_DoiChieu.Checked == true)
                            {
                                dataGridViewX1.DataSource = ds.Tables[0];
                            }
                            
                            
                        }
                        else
                            MessageBox.Show("Không có dữ liệu");
                    }
                    else
                        MessageBox.Show("Nhập thiếu Ngày");
                
                
                }
                if (cb_KhuVucRp.Text == "")
                    MessageBox.Show("Chọn Khu Vực cần xem");
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_exportExcel_Click(object sender, EventArgs e)
        {
            Search_Report();
        }

        private void cb_MaSPRp_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process prs = new Process();
            FormProcess Frp = new FormProcess();
            MaSP1 = prs.substring_right(cb_MaSPRp.Text, child);
            cb_MaCTRp.Items.Clear();
            cb_MaCTRp.Text = "";
            string sql_load = @"select MaChiTiet,TenChiTiet from dbo.ChiTietSP where MaSP='" + MaSP1 + "'";
            Frp.cb1(cb_MaCTRp, sql_load, 1, 0);

        }

        private void dataGridView2_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView2.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridView2.CurrentCell.RowIndex;
                    dataGridView2.Rows.RemoveAt(r);
                    //MessageBox.Show("aaaa");
                    int j = dataGridView2.Rows.Count - 1;
                    dataGridView2.CurrentCell = dataGridView2.Rows[j].Cells["MaCT"];
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
        }
        public void checktext_number(TextBox txt)
        {
            try
            {
                if (txt.Text.Length > 0)
                {
                    txt.Text = double.Parse(txt.Text).ToString();//ép kiểu float,nếu xảy ra ngoại lệ thì bẫy lỗi ở hàm checktext_number
                    txt.SelectionStart = txt.Text.Length;
                }
            }
            catch (Exception ex)//Bẫy lỗi khi nhập chữ vào text box
            {
                string s = "Input string";
                if (ex.Message.ToString().Contains(s))
                {
                    MessageBox.Show("Giá trị nhập phải là số");
                    txt.Text = "";
                }
                else
                    MessageBox.Show(ex.Message);
            }
        }

        

       

        
        
    }

}
